package zabbix

import (
	"encoding/json"
	"errors"
)

func ItemprototypeCreateData() (string,map[string]interface{}) {
	method := "itemprototype.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Free disk space on $1",
        "key_": "vfs.fs.size[{#FSNAME},free]",
        "hostid": "10197",
        "ruleid": "27665",
        "type": 0,
        "value_type": 3,
        "interfaceid": "112",
        "delay": "30s"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ItemprototypeDeleteData() (string,map[string]interface{}) {
	method := "itemprototype.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "27352",
        "27356"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ItemprototypeGetData() (string,map[string]interface{}) {
	method := "itemprototype.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "discoveryids": "27426"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ItemprototypeUpdateData() (string,map[string]interface{}) {
	method := "itemprototype.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "itemid": "27428",
        "interfaceid": "132"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func ItemprototypeCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ItemprototypeCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ItemprototypeDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ItemprototypeDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ItemprototypeGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ItemprototypeGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ItemprototypeCUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ItemprototypeUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



